package sfsoftware.game.actor.enemy;

import sfsoftware.game.trevor.Enemy;
import sfsoftware.util.Actor;
import sfsoftware.util.Clock;
import sfsoftware.util.Tile;
import sfsoftware.util.Updatable;
import sfsoftware.util.graphics.Graphics;

public class EnergyOrb extends Enemy implements Updatable 
{	
	protected int motion = 0;
	
	public static final int CLOCKWISE = 0;
	public static final int COUNTER_CLOCKWISE = 1;
	
	private boolean fadeState = false;
	private float fadeValue = 0;
	
	private Clock hazeClock = new Clock(10);
	private int hazeRadius;
	private float hazeFloat;
	
	public EnergyOrb(int destX, int destY, String texture, int destWidth, int destHeight, int motion, Tile[][] assocTileMap) 
	{
		super(destX, destY, texture, destWidth, destHeight);
		
		tileMap = assocTileMap;	
		this.motion = motion;		
		setSpeed(2);
		setPain(50);
		
		hazeRadius = (int) (getWidth() / 2 + Math.sin(0) * 2) + 2;
	}

	private int changeDirection()
	{
		int newDirection = 0;
		
		if(motion == CLOCKWISE)
		{
			switch(direction)
			{
				case LEFT_DIRECTION:
					newDirection = UP_DIRECTION;
					break;
				case UP_DIRECTION:
					newDirection = RIGHT_DIRECTION;
					break;
				case RIGHT_DIRECTION:
					newDirection = DOWN_DIRECTION;
					break;
				case DOWN_DIRECTION:
					newDirection = LEFT_DIRECTION;
					break;
			}
		}
		else if(motion == COUNTER_CLOCKWISE)
		{
			switch(direction)
			{
				case LEFT_DIRECTION:
					newDirection = DOWN_DIRECTION;
					break;
				case UP_DIRECTION:
					newDirection = LEFT_DIRECTION;
					break;
				case RIGHT_DIRECTION:
					newDirection = UP_DIRECTION;
					break;
				case DOWN_DIRECTION:
					newDirection = RIGHT_DIRECTION;
					break;
			}			
		}
		
		return newDirection;
	}	

	@Override
	public void draw()
	{
		//super.draw(width, height, getSrcX(), getSrcY(), width, height, 1, 1, 1, 0.9f);				
		Graphics.drawCircle(getX() + (getWidth() /2), getY() + (getHeight() /2), (getWidth() - 4) / 2 , 1, fadeValue, fadeValue, 0.95f, 0.45f, 0, 0, 0.8f);
		Graphics.drawCircle(getX() + (getWidth() /2), getY() + (getHeight() /2), hazeRadius, 1, 1, 1, 0, 1, 1, 1, 0.2f);
		Graphics.drawCircle(getX() + (getWidth() /2), getY() + (getHeight() /2), (getWidth() - 4) / 2 , 1, 0, 0, 0.15f, 1, 0, 0, 0.15f);
	}		
	
	public void explode(Actor a) 
	{		
	}
	
	public boolean move(int dirX, int dirY, Tile[][] tileMap)
	{		
		boolean successfulMove = false;
		try
		{
			// Try to move!
			successfulMove = super.move(dirX, dirY, tileMap); 
			if(!successfulMove)
			{				
				// If the move was unsuccessful, change direction
				direction = changeDirection();
			}
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			// If the actor tried to move off the playing field, change direction!
			direction = changeDirection();
		}
		return successfulMove;
	}		
	
	@Override
	public void update() 
	{

		
		if(hazeClock.updateClock())
		{		

			if(fadeState)
			{
				fadeValue = fadeValue - 0.05f;
				if(fadeValue <= 0.1f)
				{
					fadeValue = 0.1f;
					fadeState = !fadeState;
				}
			}
			else
			{
				fadeValue = fadeValue + 0.05f;
				if(fadeValue >= 1)
				{
					fadeValue = 1;
					fadeState = !fadeState;
				}
			}			
			
			hazeRadius = (int) (getWidth() / 2 + Math.sin(hazeFloat) * 2) + 2; //((int) (Math.random() * 5) - 2);
			hazeFloat = hazeFloat + 0.1f;
			if(hazeFloat >= (Math.PI * 2))
			{
				hazeFloat = 0;
			}
		}
		
		switch(direction)
		{
			case DOWN_DIRECTION:
				this.move(0, 1, tileMap);
				break;
			case UP_DIRECTION:
				this.move(0, -1, tileMap);
				break;
			case RIGHT_DIRECTION:				
				this.move(1, 0, tileMap);
				break;
			case LEFT_DIRECTION:
				this.move(-1, 0, tileMap);
				break;				
		}
	}

	@Override
	public void wiffaHit() 
	{
	
	}	
	
}
